﻿package net.wangit.framework.config;

import net.wangit.framework.exception.ConfigurationException;
import net.wangit.util.Checker;
import net.wangit.util.FileUtil;
import net.wangit.util.StringUtil;
import net.wangit.util.XMLUtil;
import net.wangit.view.unit.grid.Wheel;

import org.dom4j.Document;
import org.dom4j.Element;


public class DomainSettingsFactory {

	private DomainSettingsFactory() {
	}

	public static DomainSettings getDomainSettings(String path,
			DomainSettings defaultSettings) {
		if (!FileUtil.exists(path))
			return defaultSettings;
		try {
			DomainSettings ds;
			Document document = XMLUtil.parseByPath(path);
			Element root = document.getRootElement();
			ds = new DomainSettings();
			ds.setIndexUrl(XMLUtil.getTextString(root, "index-url",
					defaultSettings.getIndexUrl()));
			ds.setDatasource(XMLUtil.getTextString(root, "datasource",
					defaultSettings.getDatasource()));
			Element style = root.element("style");
			ds.setSkin(XMLUtil.getTextString(style, "skin", defaultSettings
					.getSkin()));
			ds
					.setFramesetTemplate(XMLUtil.getTextString(style,
							"frameset-template", defaultSettings
									.getFramesetTemplate()));
			ds.setPageTemplate(XMLUtil.getTextString(style, "page-template",
					defaultSettings.getPageTemplate()));
			ds.setUnitTemplate(XMLUtil.getTextString(style, "unit-template",
					defaultSettings.getUnitTemplate()));
			Element action = root.element("action");
			ds.setDefaultProxy(XMLUtil.getTextString(action, "default-proxy",
					defaultSettings.getDefaultProxy()));
			ds.setDefaultAction(XMLUtil.getTextString(action, "default-action",
					defaultSettings.getDefaultAction()));
			Element windowTitle = root.element("window-title");
			ds.setWindowTitlePrefix(XMLUtil.getTextString(windowTitle,
					"prefix", defaultSettings.getWindowTitlePrefix()));
			ds.setWindowTitleSuffix(XMLUtil.getTextString(windowTitle,
					"suffix", defaultSettings.getWindowTitleSuffix()));
			Element list = root.element("list");
			ds.setListSize(XMLUtil.getTextInt(list, "default-size",
					defaultSettings.getListSize()));
			ds.setPagesMax(XMLUtil.getTextInt(list, "pages-max",
					defaultSettings.getPagesMax()));
			String rowConfigStr = XMLUtil.getTextString(list, "rows-config",
					null);
			if (Checker.isEmpty(rowConfigStr))
				ds.setRowsConfig(defaultSettings.getRowsConfig());
			else
				ds
						.setRowsConfig(new Wheel(StringUtil.split(rowConfigStr,
								",")));
			Element validation = root.element("validation");
			ds.setMarkInvalidElements(XMLUtil.getTextBoolean(validation,
					"mark", defaultSettings.isMarkInvalidElements()));
			String borderColor = XMLUtil.getTextString(validation,
					"border-color", null);
			String backgroundColor = XMLUtil.getTextString(validation,
					"background-color", null);
			if (!Checker.isEmpty(borderColor)
					|| !Checker.isEmpty(backgroundColor)) {
				StringBuffer markStyle = new StringBuffer(" style=\"");
				if (!Checker.isEmpty(borderColor))
					markStyle.append("border:").append(borderColor).append(
							" 1px solid;");
				if (!Checker.isEmpty(backgroundColor))
					markStyle.append("background-color:").append(
							backgroundColor).append(";");
				markStyle.append("\"");
				ds.setMarkStyle(markStyle.toString());
			} else {
				ds.setMarkStyle(defaultSettings.getMarkStyle());
			}
			Element format = root.element("format");
			ds.setDateFormat(XMLUtil.getTextString(format, "date",
					defaultSettings.getDateFormat()));
			ds.setTimeFormat(XMLUtil.getTextString(format, "time",
					defaultSettings.getTimeFormat()));
			ds.setTimestampFormat(XMLUtil.getTextString(format, "timestamp",
					defaultSettings.getTimestampFormat()));
			return ds;
		} catch (Exception e) {
			throw new ConfigurationException(path, e);
		}
	}
}
